package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.ChannelDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.CertificateType;
import com.jyy.prefabricated.entity.Channel;
import com.jyy.prefabricated.service.ChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 渠道表(Channel)表控制层
 *
 * @author mrl
 * @since 2021-07-17 14:51:54
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ChannelController {
    /**
     * 服务对象
     */
    @Resource
    private ChannelService channelService;
    /**
     * 分页查询渠道
     * @param channelDto
     * @return
     */
    @GetMapping("/channel")
    public AjaxResult selectByParams(ChannelDto channelDto){
        log.info("Rest request to find channelDto by params channelDto", channelDto);
        PageHelper.startPage(channelDto.getPageNum(), channelDto.getPageSize());
        List<ChannelDto> channel = channelService.selectByParams(channelDto);
        PageInfo<ChannelDto> result = new PageInfo<>(channel);
        return AjaxResult.success(result);
    }
    /**
     * 新增渠道
     * @param channelDto
     * @return
     */
    @PostMapping("/channel")
    public AjaxResult insert(@Valid @RequestBody ChannelDto channelDto){
        log.info("Rest request to insert channelDto:{}",channelDto);
        Channel result = channelService.insert(channelDto);
        return AjaxResult.success(result);
    }

    /**
     * 更新channel
     * @param channel
     * @return
     */
    @PutMapping("/channel")
    public AjaxResult update(@Valid @RequestBody Channel channel){
        log.info("Rest request to update channel:{}",channel);
        channelService.update(channel);
        Channel result = channelService.queryById(channel.getChannelId());
        return AjaxResult.success(result);
    }

    /**
     * 批量删除channelDto
     * @param channelDto
     * @return
     */
    @DeleteMapping("/channel-batch")
    public AjaxResult deleteById(@RequestBody ChannelDto channelDto){
        log.info("Rest request to batch-delete channelDto by id:{}",channelDto);
        int row = channelService.batchDelete(channelDto.getIds());
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询channelDto详情
     * @param channelId
     * @return
     */
    @GetMapping("/channel/{channelId}")
    public AjaxResult selectById(@PathVariable Integer channelId){
        log.info("Rest request to find channelDto by id:{}",channelId);
        Channel result = channelService.queryById(channelId);
        return AjaxResult.success(result);
    }
    @GetMapping("/channelDetail/{channelId}")
    public AjaxResult selectDetail(@PathVariable Integer channelId){
        log.info("Rest request to find channelDto by id:{}",channelId);
        ChannelDto result = channelService.selectDetail(channelId);
        return AjaxResult.success(result);
    }
}